package com.easidea.tim.flex.remote.system
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.system.permission.Role;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.*;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;

	public class RoleMangerDao extends BaseDao
	{
		private var _roleService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var  _instance:RoleMangerDao;
		public static function getInstance():RoleMangerDao {
			if(_instance == null) {
				_instance = new RoleMangerDao()
			}
			return _instance;
		}
		public function RoleMangerDao()
		{
			super();
			_roleService.channelSet = this.buildChannelSet();
			_roleService.destination = "roleService";
			_roleService.concurrency = "last";
			_roleService.makeObjectsBindable = true;	
			_roleService.showBusyCursor = true;		
		}
		public function getRoleListByName(name:String,page:Page):void {
			_roleService.addEventListener(ResultEvent.RESULT,getRoleListByNameSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.getRoleList(name,page);
		}
		private function getRoleListByNameSuccess(e:ResultEvent):void {
			_roleService.removeEventListener(ResultEvent.RESULT,getRoleListByNameSuccess);
			if(e.result != null) {
				Env.dispatchEvent(new CustomerEvent("getRoleListSuccess",e.result));
			}
		}
		public function getAllPermissions():void {
			_roleService.addEventListener(ResultEvent.RESULT,getAllPermissionsSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.getAllPermissions();
		}
		private function getAllPermissionsSuccess(e:ResultEvent):void {
			_roleService.removeEventListener(ResultEvent.RESULT,getAllPermissionsSuccess);
			if(e.result != null) {
				Env.dispatchEvent(new CustomerEvent("getAllPermissionsSuccess",e.result));
			}
		}
		public function saveOrUpdate(role:Role):void {
			_roleService.addEventListener(ResultEvent.RESULT,saveOrUpdateSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.saveOrUpdate(role);
		}
		private function saveOrUpdateSuccess(e:ResultEvent):void {
			_roleService.removeEventListener(ResultEvent.RESULT,saveOrUpdateSuccess);
			if(e.result != null) {
				if(e.result == true) {
					AppAlert.show("保存成功","提示信息");
					Env.dispatchEvent(new CustomerEvent("saveOrUpdateSuccess",e.result));
				}else {
					AppAlert.show(e.token.message.toString(),"提示信息");
				}								
			}
		}
		public function checkDelRole(id :int) :void {
			_roleService.addEventListener(ResultEvent.RESULT,checkDelRoleSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.checkDelRole(id);
		}
		private function checkDelRoleSuccess(e:ResultEvent):void {
			_roleService.removeEventListener(ResultEvent.RESULT,checkDelRoleSuccess);
			if(e.result != null) {
				if(e.result == false) {
					AppAlert.show("该角色已被分配,不能删除","提示信息");
					return;
				} else {
				 Env.dispatchEvent(new CustomerEvent("checkDelRoleSuccess",e.result));	
				}
				
			}
		}	
		public function deleteRole(id:int):void {
			_roleService.addEventListener(ResultEvent.RESULT,deleteRoleSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.deleteRole(id);
		}
		private function deleteRoleSuccess(e:ResultEvent):void {
			_roleService.removeEventListener(ResultEvent.RESULT,deleteRoleSuccess);
			if(e.result != null) {
								if(e.result == true) {
					AppAlert.show("删除成功","提示信息");
					Env.dispatchEvent(new CustomerEvent("deleteRoleSuccess",e.result));
				}else {
					AppAlert.show(e.token.message.toString(),"提示信息");
				}
				
			}
		}	
		
		public function getChange():void{
			_roleService.addEventListener(ResultEvent.RESULT,getChangeSuccess);
			_roleService.addEventListener(FaultEvent.FAULT,_Fault);
			_roleService.returnUrl();
		}
		
		private function getChangeSuccess(e:ResultEvent):void{
			_roleService.removeEventListener(ResultEvent.RESULT,getChangeSuccess);
			if(e.result != null) {
				Env.dispatchEvent(new CustomerEvent("getChangeSuccess",e.result));
			}
		}
	}
}